package com.chisheng.service;

import com.chisheng.mapper.UserDao;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class UserService {

    private final UserDao userDao;

    public UserService(UserDao userDao) {
        this.userDao = userDao;
    }

    public void normalInsertUser() {
        boolean result = userDao.randomInsertUser();
        if (result) {
            System.out.println("Insert a user successfully.");
        }
    }

    @SuppressWarnings("all")  /* Numeric overflow in expression */
    public void insertUserHasException() {

        /* 运行出现了异常但是数据插入到了数据库中 */

        boolean result = userDao.randomInsertUser();
        int i = 1 / 0;
        if (result) {
            System.out.println("Insert a user successfully.");
        }
    }

    @Transactional
    @SuppressWarnings("all")  /* Numeric overflow in expression */
    public void insertUserHasExceptionAndTranctional() {

        /* 运行过程中出现了异常，但是进行了回滚 */

        boolean result = userDao.randomInsertUser();
        int i = 1 / 0;
        if (result) {
            System.out.println("Insert a user successfully.");
        }
    }

}
